package ils.authentication;

import ils.services.AuthenticationService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author: Rejeev Divakaran
 * Date: 7/20/13
 */
public class AuthenticationFilter implements Filter {
    private static final Logger LOGGER = LoggerFactory.getLogger(AuthenticationFilter.class);

    public void destroy() {
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        /*HttpServletRequest httpServletRequest = (HttpServletRequest)req;
        HttpServletResponse httpServletResponse = (HttpServletResponse)resp;
        String uri = httpServletRequest.getRequestURI();
        LOGGER.debug("Request URI: " + uri);
        if(uri.startsWith(httpServletRequest.getContextPath() + "/authentication")) chain.doFilter(req, resp);
        else {
            String sessionId = httpServletRequest.getSession().getId();
            if(AuthenticationService.check(sessionId)) chain.doFilter(req, resp);
            else {
                String loginUri = httpServletRequest.getContextPath() + "/authentication/login?targetUri=" + uri;
                httpServletResponse.sendRedirect(loginUri);
            }
        }*/
        chain.doFilter(req, resp);
    }

    public void init(FilterConfig config) throws ServletException {

    }

}
